import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 暗言术 - 罪
 **/
export default class MS_InsinuationCrime extends AbilityBase {
    static instance: MS_InsinuationCrime;
    buffType: AppBuffType = {
        id: 'buff_MS_InsinuationCrime',
        art: this.art,
        name: this.abilityName,
        ubertip: '持续造成暗系伤害，并在结束时结算期间受到的所有伤害',
        dur: 12,
        targetAttach: 'foot',
        targetArt: 'UI\\Feedback\\SelectionCircleEnemy\\SelectionCircleEnemy.mdl',
        interval: 3,
        onInterval: this.onBuffInterval.bind(this),
        onDestroy: this.onBuffDestroy.bind(this),
    };
    constructor(id: string) {
        super(id);
        MS_InsinuationCrime.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeDamage(this.onDamage.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BuffCommon.addBuff({
            target,
            caster: source,
            ablityId: this.id,
            buffTypeId: this.buffType.id,
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onBuffInterval(buff: Buff) {
        let source = buff.getCaster();
        let lv = this.getAbilityLevel(source);
        let dmg = 30 * lv + UnitCommon.取法强(source) * (0.3 + 0.25 * lv);
        DamageSystemInstance.applyDarkDamage(buff.getParent(), source, dmg, this.id, true);
        StunSystemIntance.stun(buff.getParent(), 0.75, source);
    }
    onBuffDestroy(buff: Buff) {
        if (IsUnitDeadBJ(buff.getParent())) return;
        let v = this._damagePool.get(buff.getParent());
        DamageSystemInstance.applyDarkDamage(buff.getParent(), buff.getCaster(), v.value, this.id);
        DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Human\\MarkOfChaos\\MarkOfChaosDone.mdl', buff.getParent(), 'overhead'));
        this._damagePool.delete(buff.getParent());
    }
    private _damagePool: Map<unit, { source: unit; value: number }> = new Map();
    onDamage(d: DamageEventData) {
        if (!BuffUtil.hasUnitBuff(d.target, this.buffType.id)) return;
        let v = this._damagePool.get(d.target) ?? { source: d.source, value: 0 };
        if (v.value == 0) {
            v.value = d.lastDamage;
            this._damagePool.set(d.target, v);
        } else {
            if (GetOwningPlayer(v.source) == GetOwningPlayer(d.source)) {
                v.value += d.lastDamage;
                this._damagePool.set(d.target, v);
            }
        }
    }
}
